import axios from 'axios';
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { toast } from 'react-toastify';

// 创建 axios 的实例对象
let instance = axios.create({
  baseURL: 'http://127.0.0.1:3001'
});

//为 instance 添加请求拦截器        config 是配置对象
instance.interceptors.request.use(config => {
  //开启进度条效果
  NProgress.start();
  //必须要返回 config 对象
  return config;
}, error => {
  throw error;
}); 

//为 instance 添加响应拦截器  {status, statusText, headers, data, config, request}
instance.interceptors.response.use(response => {
  //关闭进度条效果
  NProgress.done();
  return response.data;
}, error=>{
  //错误的统一处理
  toast.error('请求异常, 请稍后再试~~~');
  throw error;
});

export default instance;